package com.party.core.service.partner.biz;

import com.party.core.model.member.MemberGroup;
import com.party.core.model.partner.MemberPartner;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.partner.IMemberPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: yangshoukun
 * @Date: 2018/7/3 15:53
 */
@Service
public class MemberPartnerBizService {
    @Autowired
    private IMemberPartnerService memberPartnerService;

    @Autowired
    private IMemberGroupService memberGroupService;

    public MemberPartner getSet(MemberPartner memberPartner) {
        if (null == memberPartner) {
            return null;
        }
        MemberPartner db = memberPartnerService.findByMemberId(memberPartner.getMemberId());
        if (null != db) {
            return db;
        }
        memberPartnerService.insert(memberPartner);
        return memberPartner;
    }

    /**
     * 初始化
     */
    public void init() {
        List<MemberGroup> members = memberGroupService.getAllPartnerNoOpenStatus();
        members.forEach(member -> {
            MemberPartner memberPartner = new MemberPartner();
            memberPartner.setMemberId(member.getId());
            memberPartner.setPartnerType(MemberPartner.PARTNER_TYPE_DEFAULT);
            memberPartner.setCreateBy(member.getId());
            memberPartner.setUpdateBy(member.getId());
            getSet(memberPartner);
        });
    }

    /**
     * 保存或更新全部子账号的memberPartner
     *
     * @param partner 父账号的memberPartner
     * @author yifeng
     * @date 11:27 2019/9/27
     **/
    public void saveOrUpdateChildMemberPartner(MemberPartner partner) {
        new Thread(() -> {
            try {
                List<String> childIds = memberGroupService.getChild(partner.getMemberId());
                for (String childId : childIds) {
                    MemberPartner memberPartner = memberPartnerService.findByMemberId(childId);
                    if (null != memberPartner) {
                        memberPartner.setSystemType(partner.getSystemType());
                        memberPartner.setPartnerType(partner.getPartnerType());
                        memberPartner.setPartnerRemark(partner.getPartnerRemark());
                        memberPartnerService.update(memberPartner);
                    } else {
                        memberPartner = new MemberPartner();
                        memberPartner.setMemberId(childId);
                        memberPartner.setCreateBy(childId);
                        memberPartner.setUpdateBy(childId);
                        memberPartner.setSystemType(partner.getSystemType());
                        memberPartner.setPartnerType(partner.getPartnerType());
                        memberPartner.setPartnerRemark(partner.getPartnerRemark());
                        memberPartnerService.insert(memberPartner);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
